<?php

if (!defined('BASEPATH'))
	exit('No direct script access allowed');
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of upload
 *
 * @author b4by
 */
class upload extends MY_Controller {
	
	private $youtube_reg = '/https?:\/\/(?:www\.)?youtu(?:be\.com\/watch\?v=|\.be\/)([A-Za-z0-9_-]+)(&(amp;)?[\w\?=]*)?/';
	function __construct() {
		parent::__construct();
		$this->load->helper('date');
		$this->data['upload_rules'] = $this->siteConfig->upload_rules;
		if (!$this->isRootAdmin()){
			$this->data['videos_max'] = $this->getUploadLimit(TRUE);
			$this->data['images_max'] = $this->getUploadLimit();
		}
	}

	public function index() {
//		echo strtotime(date('Y-m-d',now())) . '-' . now();die;
		$json = array(
			'state' => '',
		);
		if ($this->user->last_image_upload < get_current_date())
			$this->user->today_images_uploaded = 0;
		if (!$this->isRootAdmin() && $this->user->today_images_uploaded >= $this->data['images_max']) {
			$json = array(
				'state' => 'error',
				'message' => lang('upload_image_limited'),
				'error' => array('file' => $this->upload_error)
			);
		} else if ($this->input->post('submit')) {
			$this->load->helper('form');
			$this->load->library('form_validation');
//			pr($_POST);die;
			$this->form_validation->set_rules('title', 'lang:image_title', 'required|max_length[' . $this->siteConfig->image_title_length . ']');
			if ($this->form_validation->run()) {
				if (isset($_FILES['file'])) {
					$image = $this->uploadImage('file');
					if ($image !== false) {
						$this->load->model('model_image');
						$this->load->helper('date_helper');
						
						$text_image = $this->input->post('text_image')==1?1:0;
						$image_id = $this->model_image->create(array(
							'user_id' => $this->user->id,
							'path' => $image->path,
							'thumb' => $image->thumb,
							'thumb2' => $image->thumb2,
							'upload_date' => now(),
							'last' => now(),
							'title' => $this->input->post('title'),
							'text_image'	=> $this->user->upload_text_image?$text_image:0,
						));
						$this->user->uploads++;
						$this->user->today_images_uploaded++;
						$this->model_user->update($this->user->id, array(
							'uploads' => $this->user->uploads,
							'last_image_upload' => now(),
							'today_images_uploaded' => $this->user->today_images_uploaded,
						));
						$json = array(
							'state' => 'success',
							'message' => lang('upload_success','<a href="'. url_create('site','photo','index',$image_id) .'" alt="">' . url_create('site','photo','index',$image_id) . '</a>'),
						);
					} else {
						$json = array(
							'state' => 'error',
							'message' => '',
							'error' => array('file' => $this->upload_error)
						);
					}
				} else {
					$json = array(
						'state' => 'error',
						'message' => '',
						'error' => array('file' => '<p>' . lang('image_required') . '</p>')
					);
				}
			} else {
				$error = array();
				foreach ($_POST as $key => $value)
					$error[$key] = form_error($key);
				$json = array(
					'state' => 'error',
					'message' => '',
					'error' => $error
				);
			}
		}
		$this->data['upload'] = 1;
		$this->data['upload_tip'] = $this->siteConfig->upload_tip;
		$this->data['json'] = json_encode($json);
		$this->data['temp'] = 'upload/index';
		$this->loadView();
	}

	public function video() {
		$json = array(
			'state' => '',
		);
		if ($this->user->last_video_upload < get_current_date())
			$this->user->today_videos_uploaded = 0;
		if (!$this->isRootAdmin() && $this->user->today_videos_uploaded >= $this->data['videos_max']) {
			$json = array(
				'state' => 'error',
				'message' => lang('upload_image_limited'),
				'error' => array('file' => $this->upload_error)
			);
		}else if ($this->input->post('submit')) {
			if ($this->user->active == 0) {
				$json = array(
					'state' => 'error',
					'message' => lang('account_inactived'),
				);
			} else if (!$this->isRootAdmin() && $this->user->today_images_uploaded >= $this->getUploadLimit(TRUE)) {
				$json = array(
					'state' => 'error',
					'message' => lang('upload_image_limited'),
				);
			} else {
				$this->load->helper('form');
				$this->load->library('form_validation');

				$this->form_validation->set_rules('title', 'lang:image_title', 'required|max_length[' . $this->siteConfig->image_title_length . ']');
				$this->form_validation->set_rules('video_link', 'lang:video_link', 'required|callback__check_video_link');
				if ($this->form_validation->run()) {

					$matches = array();
					preg_match($this->youtube_reg, $this->input->post('video_link'), $matches);
//					pr($matches);die;
					$video_id = $matches[1];
					$video_thumb = 'http://img.youtube.com/vi/' . $video_id . '/0.jpg';
					$video_embed = 'http://www.youtube.com/embed/' . $video_id . '?wmode=opaque';
					$this->load->model('model_image');
					$image_id = $this->model_image->create(array(
						'user_id' => $this->user->id,
						'path' => $video_embed,
						'thumb' => $video_thumb,
						'thumb2' => $video_thumb,
						'upload_date' => now(),
						'last' => now(),
						'title' => $this->input->post('title'),
						'video' => 1,
						'video_id'	=> $video_id
					));
					$this->user->uploads++;
					$this->user->today_videos_uploaded++;
					$this->model_user->update($this->user->id, array(
						'uploads' => $this->user->uploads,
						'last_video_upload' => now(),
						'today_videos_uploaded' => $this->user->today_videos_uploaded,
					));
					$json = array(
						'state' => 'success',
						'message' => lang('video_post_success','<a href="'. url_create('site','photo','index',$image_id) .'" alt="">' . url_create('site','photo','index',$image_id) . '</a>'),
					);
				} else {
					$error = array();
					foreach ($_POST as $key => $value)
						$error[$key] = form_error($key);
					$json = array(
						'state' => 'error',
						'message' => '',
						'error' => $error
					);
				}
			}
		}
		
		$this->data['upload_video'] = 1;
		$this->data['json'] = json_encode($json);
		$this->data['upload_tip'] = $this->siteConfig->video_upload_tip;
		$this->data['temp'] = 'upload/video';
		$this->loadView();
	}

	public function _check_video_link($value) {
		if (preg_match($this->youtube_reg, $value) != 1) {
			$this->form_validation->set_message(__FUNCTION__, lang('invalid_video_link'));
			return FALSE;
		}
		return TRUE;
	}

	protected function getUploadLimit($video = FALSE) {
		$this->load->model('model_limit');
		$input = array();
		$input['where'] = array('likes <=' => $this->user->likes);
		if ($video)
			$input['order'] = array('videos_max', 'desc');
		else
			$input['order'] = array('images_max', 'desc');
		$limits = $this->model_limit->get_list($input);

		if (count($limits) > 0)
			return $video ? $limits[0]->videos_max : $limits[0]->images_max;
		return 0;
	}

}

?>
